﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ch03;

namespace ch02
{
    public partial class Form5 : Form
    {
        //定义变量保存打开的文件的路径及名称
        string fileName;
        
        public Form5()
        {
            InitializeComponent();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.WordWrap = 自动换行ToolStripMenuItem.Checked;

            /*
            if (!richTextBox1.WordWrap)
            {
                richTextBox1.WordWrap = true;
            }
            else
            {
                richTextBox1.WordWrap = false;
            }
            */
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectedText = "";
        }

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建字体对话框对象
            FontDialog fd = new FontDialog();
            //打开显示字体对话框
            if (fd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的字体，并设置到富文本框上
                //richTextBox1.Font = fd.Font;
                richTextBox1.SelectionFont = fd.Font;
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建颜色对话框对象
            ColorDialog cd = new ColorDialog();
            //打开颜色字体对话框
            if (cd.ShowDialog() == DialogResult.OK)
            {
                //将用户选择的颜色设置到富文本框中
                //richTextBox1.ForeColor = cd.Color;
                richTextBox1.SelectionColor = cd.Color;
            }
            
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //创建打开文件对话框对象
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "文本文档(*.txt)|*.txt";

            //显示打开文件对话框
            if(ofd.ShowDialog()== DialogResult.OK)
            {
                //保存用户选择的文件的路径及名称到fileName变量中
                fileName = ofd.FileName;
                //将用户选择的文件中的内容加载到富文本框中
                richTextBox1.LoadFile(fileName, RichTextBoxStreamType.PlainText);

                //richTextBox1.Text = File.ReadAllText(ofd.FileName,Encoding.UTF8);
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileName == null || fileName == "")
            {
                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Filter = "文本文档(*.txt)|*.txt";

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    fileName = sfd.FileName;
                }
            }
            if (fileName != null && fileName != "")
            {
                richTextBox1.SaveFile(fileName, RichTextBoxStreamType.PlainText);
            }
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //清空富文本框中的内容
            richTextBox1.Text = "";
            //清空之前打开的文件的路径及名称
            fileName = "";
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "文本文档(*.txt)|*.txt";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fileName = sfd.FileName;
                richTextBox1.SaveFile(fileName, RichTextBoxStreamType.PlainText);
            }
        }

        private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip1.Visible = 状态栏ToolStripMenuItem.Checked;
            
        }

        private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Form6().ShowDialog();
        }
    }
}
